#include "helper.h"
#include "WebViewWrapper.h"
#include "ResourceInterceptorWrapper.h"

#include <vcclr.h>

using namespace AwesomiumDotNet;

AwesomiumDotNet::ResourceResponse::ResourceResponse()
{
}

AwesomiumDotNet::ResourceResponse::ResourceResponse(Awesomium::ResourceResponse* response)
{
	_response = response;
}

AwesomiumDotNet::ResourceResponse^ AwesomiumDotNet::ResourceResponse::Create(array<Byte>^ buffer, String^ mimeType)
{
	pin_ptr<unsigned char> bufferPtr = &buffer[0];
	Awesomium::ResourceResponse* response = Awesomium::ResourceResponse::Create((size_t)buffer->Length, bufferPtr, MarshalString(mimeType));
	return gcnew AwesomiumDotNet::ResourceResponse(response);
}

AwesomiumDotNet::ResourceResponse^ AwesomiumDotNet::ResourceResponse::Create(String^ filePath)
{
	pin_ptr<const wchar_t> wch = PtrToStringChars(filePath);
	Awesomium::ResourceResponse* response = Awesomium::ResourceResponse::Create(wch);
	return gcnew AwesomiumDotNet::ResourceResponse(response);
}

AwesomiumDotNet::ResourceResponse::ResourceResponse(String^ filePath)
{
	pin_ptr<const wchar_t> wch = PtrToStringChars(filePath);
	Awesomium::ResourceResponse* response = new Awesomium::ResourceResponse(wch);
	return gcnew AwesomiumDotNet::ResourceResponse(response);
}

Awesomium::ResourceResponse* AwesomiumDotNet::ResourceResponse::getNative()
{
	return _response;
}


AwesomiumDotNet::ResourceRequest::ResourceRequest(Awesomium::ResourceRequest* request)
{
	_request = request;
}


AwesomiumDotNet::ResourceInterceptor::ResourceInterceptor()
{
	resourceInterceptor = new ResourceInterceptorWrapper(this);
}

AwesomiumDotNet::ResourceResponse^ AwesomiumDotNet::ResourceInterceptor::OnRequest(WebView^ caller, ResourceRequest^ request)
{
	return nullptr;
}

void AwesomiumDotNet::ResourceInterceptor::OnResponse(WebView^ caller, String^ url, int statusCode, ResourceResponseMetrics^ metrics)
{
}

ResourceInterceptorWrapper* AwesomiumDotNet::ResourceInterceptor::getNative()
{
	return resourceInterceptor;
}


void ResourceInterceptorWrapper::setOwner(AwesomiumDotNet::WebView^ owner)
{
	_owner = owner;
}

AwesomiumDotNet::ResourceInterceptorWrapper::ResourceInterceptorWrapper(AwesomiumDotNet::ResourceInterceptor^ resourceInterceptor)
{
	_resourceInterceptor = resourceInterceptor;
}

Awesomium::ResourceResponse* AwesomiumDotNet::ResourceInterceptorWrapper::onRequest(Awesomium::WebView* caller, Awesomium::ResourceRequest* request)
{
	AwesomiumDotNet::ResourceResponse^ response = _resourceInterceptor->OnRequest(_owner, gcnew ResourceRequest(request));
	if (response == nullptr)
		return NULL;

	return response->getNative();
}

void AwesomiumDotNet::ResourceInterceptorWrapper::onResponse(Awesomium::WebView* caller, const std::string& url, int statusCode, const Awesomium::ResourceResponseMetrics& metrics)
{
	_resourceInterceptor->OnResponse(_owner, gcnew String(url.c_str()), statusCode, gcnew ResourceResponseMetrics(metrics));
}


AwesomiumDotNet::ResourceResponseMetrics::ResourceResponseMetrics(const Awesomium::ResourceResponseMetrics& metrics)
{
	wasCached = metrics.wasCached;
	requestTimeMs = metrics.requestTimeMs;
	responseTimeMs = metrics.responseTimeMs;
	expectedContentSize = metrics.expectedContentSize;
	mimeType = gcnew String(metrics.mimeType.c_str());
}
